<template>
	<view class="container">
		<u-navbar title='数据统计-品牌方' :autoBack="true" leftIconColor="#1E272E" bgColor="transparent"
			:titleStyle="{fontWeight:700,fontSize:'32rpx',color:'#1E272E'}"></u-navbar>
		<image src="https://renovationcdn.jiachuangnet.com/index/workerIndex/bg.png" class='bg' style=""></image>
		<view style="position: absolute;top:180rpx;left:25rpx;width: 700rpx;min-height:160rpx;">
			<view class="picsBox">
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.waitStartCount"
							color='#fff'></u-count-to></view>
					<view>待开工</view>
				</view>
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.doingCount" color='#fff'></u-count-to>
					</view>
					<view>待完成</view>
				</view>
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.doneCount" color='#fff'></u-count-to>
					</view>
					<view>已完成</view>
				</view>
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.overdueCount" color='#fff'></u-count-to>
					</view>
					<view>已逾期</view>
				</view>
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.afterSaleCount"
							color='#fff'></u-count-to></view>
					<view>售后中</view>
				</view>
				<view>
					<view><u-count-to :startVal="0" :endVal="brandStatisticsVO.afterSaleOverdueCount"
							color='#fff'></u-count-to></view>
					<view>售后逾期</view>
				</view>
			</view>
			<view class="floor">
				<view class="flex" style="align-items: center;justify-content: space-between;margin-bottom: 30rpx;">
					<image style="width: 35rpx;height: 35rpx"
						src="https://qiniushelves.jiachuangnet.com/renovation/img/V391269日历.png"></image>
					<view class="" style="font-size: 26rpx;">选择日期：</view>
					<view class="workerSearch">
						<view @click="startShow=true">
							<u-input placeholder="开始日期" v-model='startTime' style="width: 240rpx;" :disabled="true">
							</u-input>
						</view>
						-
						<view @click="endShow=true">
							<u-input placeholder="结束日期" v-model='endTime' style="width: 240rpx;" :disabled="true">
							</u-input>
						</view>
					</view>
				</view>
				<view class="flex" style="align-items: center;justify-content: space-between;margin-bottom: 30rpx;">
					<image style="width: 35rpx;height: 35rpx"
						src="https://qiniushelves.jiachuangnet.com/renovation/img/V532639装修公司.png"></image>
					<view class="" style="font-size: 26rpx;">装修公司：</view>
					<view class="workerSearch" style="position: relative;">
						<view @click="showCompany=true" style="width: 410rpx;"> <u-input placeholder="按装修公司查询"
								v-model='companyName' style="width: 410rpx;">
							</u-input></view>
						<!-- <view
							style="width: 34rpx;height: 34rpx;text-align: center;line-height: 32rpx;border-radius: 50%;border: 2rpx solid #333;"
							v-if='companyName' @click="getClear">x</view> -->
					</view>
				</view>
				<view>
					<u-popup :show="showCompany" @close="showCompany=false" @open="open">
						<view
							style="height: 500rpx; padding: 40rpx;display: flex;flex-direction: column;overflow-y:auto;"
							v-if="companyData.length!=0">
							<view style="margin-bottom: 15rpx;" v-for="(item, index) in companyData" :key="index"
								@click="checkStore(item)">
								<view
									style="width: 15rpx;height: 15rpx;background-color:#15a29d;display: inline-block;margin-right: 15rpx;">
								</view>
								{{ item.companyName }}
							</view>
						</view>
						<view v-if="companyData.length==0">
							<u-empty mode="car"
								icon="https://qiniushelves.jiachuangnet.com/renovation/img/V725723数据为空.png"></u-empty>
						</view>
					</u-popup>
					<!-- <u-button @click="show = true">打开</u-button> -->
				</view>
				<u-calendar :show="startShow" mode="single" @confirm="chooseStartDate" :min-date="minDate"
					:max-date="maxDate" monthNum='24' @close="startShow=false" color="#15a29d" />

				<u-calendar :show="endShow" mode="single" @confirm="chooseEndDate" :min-date="minDate"
					:max-date="maxDate" monthNum='24' @close="endShow=false" color="#15a29d" />
				<view class="title">
					<text class="title_text" style="font-weight: 600;margin:10rpx 0;display: block;">门店数量</text>
					<view class="charts-box">
						<qiun-data-charts type="column" :opts="opts1" :chartData="chartData1" :ontouch="true"
							:inScrollView='true' canvas-id="line1" :canvas2d="true" />
					</view>
				</view>
				<view class="title">
					<view style="display: flex;justify-content: space-between;align-items: center;"><text
							class="title_text" style="font-weight: 600;margin:10rpx 0;display: block;">开店增长率</text>
						<view @click="provinceShow=true" style="display: flex;align-items: center;" class="province">
							<image style="width: 25rpx;height: 32rpx;margin-right: 10rpx;"
								src="https://renovationcdn.jiachuangnet.com/index/workerIndex/map.png"></image>
							<text style="display: block;">{{province||'全国'}}</text>
						</view>
					</view>
					<view class="charts-box">
						<qiun-data-charts :ontouch="true" :inScrollView='true' type="line" :opts="opts2"
							:chartData="chartData2" canvas-id="line2" :canvas2d="true" />
					</view>
				</view>
				<view class="title">
					<text class="title_text" style="font-weight: 600;margin:10rpx 0;display: block;">逾期店铺</text>
					<view class="charts-box">
						<qiun-data-charts :ontouch="true" :inScrollView='true' type="line" :opts="opts2"
							:chartData="chartData3" canvas-id="line3" :canvas2d="true" />
					</view>
				</view>
				<view class="paihang">
					<text class="title_text" style="font-weight: 600;margin:30rpx 0;display: block;">逾期率排行</text>
					<view class="content" style="margin-top: 15rpx;" v-for="item in overdueRate">
						<text style="margin:10rpx 0;display: block;">{{item.companyName}}</text>
						<u-line-progress :percentage="item.overdueRate" activeColor="#15a29d"></u-line-progress>
					</view>

				</view>

				<view class="title" style="margin-top: 50rpx;">
					<text class="title_text" style="font-weight: 600;margin:10rpx 0;display: block;">售后数量</text>
					<view class="charts-box">
						<qiun-data-charts :ontouch="true" :inScrollView='true' type="column" :opts="opts"
							:chartData="chartData4" canvas-id="line4" :canvas2d="true" />
					</view>
				</view>
				<view class="title">
					<text class="title_text" style="font-weight: 600;margin:10rpx 0;display: block;">48小时售后完成率</text>
					<view class="charts-box">
						<qiun-data-charts :ontouch="true" :inScrollView='true' type="line" :opts="opts2"
							:chartData="chartData5" canvas-id="line5" :canvas2d="true" />
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="provinceShow" mode="left" @close="provinceShow=false" minDate="2020-01-01">
			<view style="width: 400rpx;">
				<view style="padding-top: 100rpx;font-weight: 700;font-size: 28rpx;">
					请选择店铺所在城市
				</view>
				<view style="overflow-y: scroll;max-height: 80vh;margin:20rpx 0;">
					<u-radio-group iconPlacement="right" v-model="province" placement="column">
						<u-radio activeColor='#13989a' shape='square'
							:customStyle="{ borderBottom:'1rpx solid #eee',padding:'10px'}"
							v-for="(item, index) in provinceColumns" :key="index" :label="item.name" :name="item.name">
						</u-radio>
					</u-radio-group>
				</view>
				<view style="width:300rpx;margin:0 auto">
					<u-button color='#13989a' @click="searchProvince">查询</u-button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startShow: false,
				endShow: false,
				startTime: '',
				endTime: '',
				calendarShow2: false,
				brandTodayDataObj: {},
				provinceShow: false,
				province: '全部',
				companyId: "",
				companyData: [],
				pickTimeShow: false,
				showCompany: false,
				dateshow: false,
				mode: 'sigle',
				companyName: "",
				minDate: new Date('2024-01-01').getTime(),
				maxDate: new Date('2030-12-31').getTime(),
				chartDataShow: true,
				chartDataShow2: true,
				chartDataShow3: true,
				chartData1: {},
				brandStatisticsVO: {},
				overdueRate: [],
				chartData2: {},
				chartData3: {},
				chartData4: {},
				chartData5: {},
				provinceColumns: [{
						name: '全部'
					},
					{
						name: '北京市'
					},
					{
						name: '天津市'
					},
					{
						name: '河北省'
					},
					{
						name: '山西省'
					},
					{
						name: '上海市'
					},
					{
						name: '山东省'
					},
					{
						name: '河南省'
					},
					{
						name: '湖北省'
					},
					{
						name: '湖南省'
					},
					{
						name: '广东省'
					},
					{
						name: '广西壮族自治区'
					},
					{
						name: '海南省'
					},
					{
						name: '重庆市'
					},
					{
						name: '四川省'
					},
					{
						name: '贵州省'
					},
					{
						name: '云南省'
					},
					{
						name: '陕西省'
					},
				],
				opts1: {
					color: ["#3CA272", "#73C0DE", "#73C0DE", "#0da2a2"],
					padding: [15, 15, 0, 5],
					enableScroll: true,
					legend: {},
					xAxis: {
						disableGrid: true,
						labelCount: 3,
						itemCount: 5,
						scrollShow: true,
					},
					yAxis: {
						data: [{
							min: 0
						}]
					},
					extra: {
						column: {
							type: "group",
							width: 30,
							activeBgColor: "#000000",
							activeBgOpacity: 0.08,
							linearType: "custom",
							seriesGap: 5,
							linearOpacity: 0.5,
							barBorderCircle: true,
							customColor: [
								"#0da2a2",
								"#73C0DE"
							]
						}
					}
				},
				opts: {
					color: ["#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [15, 15, 0, 5],
					enableScroll: true,
					legend: {},
					xAxis: {
						disableGrid: true,
						labelCount: 3,
						itemCount: 5,
						scrollShow: true,
					},
					yAxis: {
						data: [{
							min: 0
						}]
					},
					extra: {
						column: {
							type: "group",
							width: 30,
							activeBgColor: "#000000",
							activeBgOpacity: 0.08
						}
					}
				},
				opts2: {
					color: ["#3CA272", "#3CA272", "#FC8452", "#9A60B4", "#ea7ccc"],
					padding: [15, 10, 0, 15],
					enableScroll: true,
					legend: {},
					xAxis: {
						disableGrid: true,
						labelCount: 3,
						itemCount: 5,
						scrollShow: true,
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						}
					}
				}
			}
		},
		created() {},
		onLoad() {
			uni.$u.http.get('/company/getCompanyList', {
				params: {
					pageSize: 10000,
					pageNumber: 1
				}
			}).then(res => {
				this.companyData = res.data
				if (this.companyData.length != 0) {
					this.companyName = this.companyData[0].companyName
					this.companyId = this.companyData[0].companyId
				}
			})
		},
		onReady() {
			this.getStoreDate()
			// this.getData()
		},
		computed: {

		},
		watch: {

		},
		methods: {
			open() {},
			checkboxChange(item) {
				console.log(item);
			},
			async searchProvince() {
				this.provinceShow = false;
				this.getStoreDate()
			},
			chooseStartDate(e) {
				this.startTime = e[0]
				this.startShow = false
				this.getStoreDate()
			},
			chooseEndDate(e) {
				this.endTime = e[0]
				this.endShow = false
				this.getStoreDate()
			},
			checkStore(item) {
				this.companyName = item.companyName
				this.companyId = item.companyId
				this.showCompany = false
				this.getStoreDate()
			},
			getClear() {
				this.companyName = ""
			},
			// 获取门店数量柱状图
			getStoreDate() {
				const params1 = {
					startTime: this.startTime,
					endTime: this.endTime,
					companyId: this.companyId,
					address: this.province
				}
				if (this.province == '全部') delete params1.address
				uni.$u.http.get('/assign/getBrandGrowthData', {
					params: params1
				}).then(res => {
					if (res.length != 0) {
						let storeRate = {
							categories: res.map(item => item.growthDate),
							series: [{
								name: "开店率",
								data: res.map(item => item.growthCount)
							}]
						}
						this.chartData2 = JSON.parse(JSON.stringify(storeRate));
					} else {
						let storeRate = {
							categories: [''],
							series: [{
								name: "开店率",
								data: ['']
							}]
						}
						this.chartData2 = JSON.parse(JSON.stringify(storeRate));
					}
				})
				uni.$u.http.get('/assign/getBrandDataHomeVO', {
					params: {
						startTime: this.startTime,
						endTime: this.endTime,
						companyId: this.companyId
					}
				}).then(res => {
					this.brandStatisticsVO = res.brandStatisticsVO;
					this.overdueRate = res.overdueRate;

					const generateChartData = (list, categoryKey, valueKey, name) => {
						if (Array.isArray(list) && list.length > 0) {
							return {
								categories: list.map(item => item[categoryKey]),
								series: [{
									name,
									data: list.map(item => item[valueKey])
								}]
							};
						} else {
							return {
								categories: [''],
								series: [{
									name,
									data: ['']
								}]
							};
						}
					};

					// 门店数量图表
					this.chartData1 = generateChartData(res.storeCount, 'storeDate', 'storeCount', '数量');

					// 逾期店铺图表
					this.chartData3 = generateChartData(res.overdueDateCount, 'growthDate', 'growthCount',
						'逾期店铺数量');

					// 售后数量图表
					this.chartData4 = generateChartData(res.afterSaleCount, 'growthDate', 'growthCount', '售后数量');

					// 售后完成率图表
					this.chartData5 = generateChartData(res.afterSaleOverdueRate, 'overdueDate',
						'afterSaleOverdueRate', '售后率');
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.charts-box {
		width: auto;
		height: 400rpx;
		margin-top: 10rpx;
	}

	.title {
		margin-top: 20rpx;
	}

	canvas {
		z-index: 1 !important;
		/* 设置一个较低的层级 */
		position: relative !important;
		/* 确保它不是 absolute/fixed */
	}

	.container {
		min-height: 100vh;
		overflow-y: scroll;
		position: relative;

		.picsBox {
			color: #fff;
			background: url('https://renovationcdn.jiachuangnet.com/bg33.png');
			background-size: cover;
			background-repeat: no-repeat;
			background-position: center;
			justify-content: space-around;
			align-items: center;
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 15px;
			width: 100%;
			border-radius: 20rpx;
			height: 250rpx;

			view {
				display: flex;
				flex-direction: column;
				align-items: center;
			}
		}

		.floor {
			border-radius: 20rpx;
			padding: 29rpx 21rpx;
			margin-top: 22rpx;
			background-color: #fff;
			min-height: 175rpx;
		}
	}

	.bg {
		width: 750rpx;
		height: 430rpx;
		position: absolute;
		top: 0;
		left: 0;
		z-index: 1;
	}

	.workerSearch {
		display: flex;
		align-items: center;
		padding-left: 22rpx;
		width: 456rpx;
		height: 70rpx;
		background: #F4F7F9;
		opacity: 0.9;
	}

	.timeConner {
		display: flex;
		background: #0da2a2;
		height: 80rpx;
		border-radius: 50rpx;
		margin-left: 50rpx;
		margin-top: 150rpx;
		margin-right: 50rpx;
		justify-content: space-evenly;
		color: #fff;
		align-items: center;
		border: 10rpx solid #15a29d;
	}

	.bottom {
		display: flex;
		justify-content: center;
		margin-top: 30rpx;
		padding-left: 120rpx;
		padding-right: 120rpx;
	}

	.storeNameStyle {
		max-height: 500rpx;
		overflow: auto;
		width: 100%;
		background-color: #F5F5F5;
		z-index: 10000;
		top: 70rpx;
		left: 0;
		position: absolute;

		span {
			display: flex;
			margin-left: 50rpx;
			margin-top: 20rpx;
			padding-bottom: 10rpx;
		}
	}
</style>